﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace LunwenYun.Infrastructure.Entities.Repository
{
    [Table("T_TradeHistory")]
    public class T_TradeHistory : IEntity<T_TradeHistory>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        public int TradeId { get; set; }

        public int PreStatus { get; set; }

        public int NxtStatus { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string Remark { get; set; }

        public DateTime RecordDate { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string Operator { get; set; }

        public override T_TradeHistory Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_TradeHistory Find(System.Linq.Expressions.Expression<Func<T_TradeHistory, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.TradeHistory.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.NxtStatus = item.NxtStatus;
                    this.PreStatus = item.PreStatus;
                    this.RecordDate = item.RecordDate;
                    this.Remark = item.Remark;
                    this.TradeId = item.TradeId;
                    this.Operator = item.Operator;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_TradeHistory, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.TradeHistory.Any(where);
            }
        }
    }
}
